package com.zwps.common.api.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 自定义参数选项校验器
 * 
 * @ClassName: OptionValidator
 * @Description:
 * @author lyh
 * @date 2023年5月23日 下午3:17:14
 *
 */
public class StringOptionValidator implements ConstraintValidator<StringOption, CharSequence> {
    private CharSequence[] options;

    @Override
    public void initialize(StringOption option) {
        options = option.value();
    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (StrUtil.isEmpty(value)) {
            return true;
        }
        if (options == null || options.length == 0) {
            return false;
        }
        for (CharSequence option : options) {
            if (option != null && ObjectUtil.equal(option, value)) {
                return true;
            }
        }
        return false;
    }

}
